﻿using CommandService.Common;
using DBService;
using DBService.Handler;
using Entity;
using Entity.SysEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandService
{
    public class Commands : Commons
    {

        DbHandler ser;
        public Commands()
        {

            ser = new DbHandler();
        }
        /// <summary>
        /// 根据实体对象查询数据列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual List<T> GetDataByObject<T>(object obj)
        {
            return this.TableTolist<T>(ser.Query(obj));
        }

        /// <summary>
        /// 添加方法(单表)
        /// </summary>
        /// <param name="obj">对象模型</param>
        /// <returns></returns>
        public ReturnResult Add(object obj)
        {
            List<OperateModel> list = new List<OperateModel>();
            OperateModel oper = new OperateModel();
            oper.Data = obj;
            oper.Types = OperateModel.OperateType.Add;
            list.Add(oper);
            return ser.TransUpdate(list);
        }
        /// <summary>
        /// 修改方法(单表)
        /// </summary>
        /// <param name="oper"></param>
        /// <returns></returns>
        public virtual ReturnResult Edit(OperateModel oper)
        {
            List<OperateModel> list = new List<OperateModel>();
            oper.Types = OperateModel.OperateType.Edit;
            list.Add(oper);
            return ser.TransUpdate(list);
        }
        /// <summary>
        /// 删除方法(单表)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual ReturnResult Del(object obj)
        {
            List<OperateModel> list = new List<OperateModel>();
            OperateModel oper = new OperateModel();
            oper.Data = obj;
            oper.Types = OperateModel.OperateType.Delete;
            list.Add(oper);
            return ser.TransUpdate(list);
        }
        #region 多表事物更新
        /// <summary>
        /// 多表事物更新
        /// </summary>
        /// <param name="list">操作集合</param>
        /// <returns></returns>
        public ReturnResult MoreUpdate(List<OperateModel> list)
        {
            return ser.TransUpdate(list);
        }
        #endregion

        /// <summary>
        /// 多表事物更新
        /// </summary>
        /// <param name="list">sql集合</param>
        /// <returns></returns>
        public ReturnResult MoreUpdate(List<string> list)
        {
            return ser.TransUpdate(list);
        }

        /// <summary>
        /// 单表更新方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public virtual ReturnResult UpdateSql(string sql)
        {
            return ser.UpdataBySql(sql);
        }

        /// <summary>
        /// 根据sql查询语句获取数据表格
        /// </summary>
        /// <param name="sql">执行sql</param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            return ser.QueryBySql(sql);
        }


        /// <summary>
        /// 分页数据查询
        /// </summary>
        /// <typeparam name="T">目标数据模型</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">当前页面显示信息数</param>
        /// <param name="sqlWhere">Where条件字符串</param>
        /// <param name="orderStr">排序字段</param>
        /// <param name="pageCount">信息总数</param>
        /// <returns>list数据集合</returns>
        public virtual List<T> PageList<T>(string tableName, string pageIndex, string pageSize, string sqlWhere, string orderStr, ref string pageCount, string fileds = null)
        {
            return this.TableTolist<T>(ser.PageList(tableName, pageIndex, pageSize, sqlWhere, orderStr, ref pageCount, fileds));
        }
        /// <summary>
        /// 分页查询方法
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlWhere"></param>
        /// <param name="orderStr"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public virtual List<Dictionary<string, string>> PageList(string tableName, string pageIndex, string pageSize, string sqlWhere, string orderStr, ref string pageCount, string fileds = null)
        {
            return this.TableToDictionary(ser.PageList(tableName, pageIndex, pageSize, sqlWhere, orderStr, ref pageCount, fileds));
        }
        /// <summary>
        /// 分页查询方法（返回DataTable）
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sqlWhere"></param>
        /// <param name="orderStr"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public virtual DataTable PageTableList(string tableName, string pageIndex, string pageSize, string sqlWhere, string orderStr, ref string pageCount, ref string paegNumber, string fileds = null)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("( ");
            sb.Append(tableName);
            sb.Append(" ) tem ");
            var table = ser.PageList(sb.ToString(), pageIndex, pageSize, sqlWhere, orderStr, ref pageCount, fileds);
            var tablePage = 0;
            tablePage = int.Parse(pageCount) / int.Parse(pageSize);
            if ((int.Parse(pageCount) % int.Parse(pageSize)) > 0)
            {
                tablePage++;
            }
            paegNumber = tablePage.ToString();
            return table;
        }
        /// <summary>
        /// 获取系统参数
        /// </summary>
        /// <param name="parCode"></param>
        /// <returns></returns>
        public List<DbSysParameter> getParam(string parCode)
        {
            DbSysParameter sys = new DbSysParameter();
            sys.parCode = parCode;
            sys.isDel = 0;
            return GetDataByObject<DbSysParameter>(sys);
        }
        /// <summary>
        /// 分页数据查询
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">当前页面显示信息数</param>
        /// <param name="pageCount">信息总数</param>
        /// <returns>DataTable数据集合</returns>
        public virtual DataTable PageTableList(string sql, string pageIndex, string pageSize, ref string pageCount, ref string pageNumber)
        {
            var dt = ser.QueryBySql(sql);
            return getPageList(dt, pageIndex, pageSize, ref pageCount, ref pageNumber);
        }
        /// <summary>
        /// 分页数据查询
        /// </summary>
        /// <param name="sql">dataTable</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">当前页面显示信息数</param>
        /// <param name="pageCount">信息总数</param>
        /// <param name="pageNumber">页总数</param>
        /// <returns>DataTable数据集合</returns>
        public virtual DataTable PageTableList(DataTable dt, string pageIndex, string pageSize, ref string pageCount, ref string pageNumber)
        {
            return getPageList(dt, pageIndex, pageSize, ref pageCount, ref pageNumber);
        }

        /// <summary>
        /// 获取DataTable分页数据
        /// </summary>
        /// <param name="dt">DataTable数据源</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">当前页面显示信息数</param>
        /// <param name="pageCount">信息总数</param>
        /// <returns></returns>
        private DataTable getPageList(DataTable dt, string pageIndex, string pageSize, ref string pageCount, ref string paegNumber)
        {
            if (dt.Rows.Count > 0)
            {
                pageCount = dt.Rows.Count.ToString();
            }
            var tablePage = 0;
            tablePage = dt.Rows.Count / int.Parse(pageSize);
            if ((dt.Rows.Count % int.Parse(pageSize)) > 0)
            {
                tablePage++;
            }
            paegNumber = tablePage.ToString();
            var tempDt = dt.Clone();
            if (int.Parse(pageIndex) > tablePage)
            {
                pageIndex = tablePage.ToString();
            }
            var start = (int.Parse(pageIndex) - 1) * int.Parse(pageSize);
            var end = 0;
            if (int.Parse(pageIndex) == tablePage)
            {
                end = dt.Rows.Count;
            }
            else
            {
                end = int.Parse(pageIndex) * int.Parse(pageSize);
            }
            if (dt.Rows.Count > 0)
            {
                for (int i = start; i < end; i++)
                {
                    var r = dt.Rows[i];
                    tempDt.Rows.Add(r.ItemArray);
                }
            }
            return tempDt;
        }
    }
}

